package com.gl.saasservice.sys.service;

import com.gl.common.vo.ApiData;
import com.gl.framework.web.response.Result;
import com.gl.saasservice.announcement.service.AnnouncementService;
import com.gl.saasservice.entity.AppUser;
import com.gl.saasservice.entity.User;
import com.gl.saasservice.house.vo.HouseVO;
import com.gl.saasservice.oss.service.OSSService;
import com.gl.saasservice.sys.Repository.FeedbackRepository;
import com.gl.saasservice.sys.entity.Feedback;
import com.gl.saasservice.sys.vo.FeedbackVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class FeedbackService {
    private static final Logger LOG = LoggerFactory.getLogger(AnnouncementService.class);


    @Autowired
    private OSSService ossService;
    /**
     * OSS 域名
     */
    @Value("${ali.oss.endpoint}")
    private String endpoint;
    /*
     *oss地址
     */
    @Value("${ali.oss.bucket}")
    private String bucket;
    @Autowired
    @Qualifier("stringRedisTemplate")
    private StringRedisTemplate redisTemplate;
    @Autowired
    private FeedbackRepository feedbackRepository;
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public Result list(String token, Integer pageNumber, Integer pageSize, Integer type) {
        List<Object> args = new ArrayList<>();
        ApiData<FeedbackVo> apiData = new ApiData<>();
        args.add(type);
        Long total = jdbcTemplate.queryForObject("select count(1) from `saas_feedback` where type = ? ", Long.class, args.toArray());
        if (total == 0) {
            return Result.success(apiData);
        }
        args.add(pageSize);
        args.add(pageNumber * pageSize);
        List<FeedbackVo> list = jdbcTemplate
                .query("select * from `saas_feedback` where type = ?  ORDER BY release_time DESC LIMIT ? OFFSET ?", new BeanPropertyRowMapper<>(FeedbackVo.class), args.toArray());
        apiData.setTotal(total);
        apiData.setData(list);
        return Result.success(apiData);
    }

    /**
     * 意见反馈消息已读
     */
    public Result readFeedback(String token, Long[] Ids) {
        try {
            for (Long id : Ids) {
                Optional<Feedback> byId = feedbackRepository.findById(id);
                Feedback feedback = byId.get();
                feedback.setStatus(1);
                feedbackRepository.save(feedback);
            }
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
            return Result.fail();
        }
        return Result.success("已处理");
    }
}
